<?php


namespace common\models\base;

use Yii;
use yii\db\ActiveRecord as BaseActiveRecord;

class ActiveRecord extends BaseActiveRecord
{

    public static function getDb()
    {
        return DatabaseManager::getDb();
    }


    public static function instance($refresh = false)
    {
        return parent::instance($refresh);
    }

    /**
     * 转换模型的错误信息数组为字符串
     * @return string
     */
    public function errorsToString()
    {
        $errors = [];
        foreach ($this->errors as $attribute => $msgs) {
            $msg = '';
            foreach ($msgs as $msg) {
                $msg .= ',';
            }
            $errors[] = trim($msg, ',');
        }
        return implode(';', array_unique($errors));
    }

    /**
     * 转换模型的错误信息数组为字符串, 只取最前面的一个
     * @return string
     */
    public function errorsToOneString()
    {
        foreach ($this->errors as $attribute => $msgs) {
            foreach ($msgs as $msg) {
                return $msg;
            }
        }
        return '';
    }
}